#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify , request,g

from service.dictionaryService import DictionaryService
from controller.dictionaryController import DictionaryController
from utils import commons
from utils.response_code import RET
from utils.verify_operate import verify_user_role

class DictionaryOtherResource(Resource):

    @classmethod
    def sensitive_update(cls, dictionary_id):
        if not dictionary_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['dictionary_id'] = dictionary_id

        res = DictionaryController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])


    # 以文本文件添加词典
    @classmethod
    def add_dictionary(cls):
        if 'file' not in request.files:
            return 'No file part'

        file = request.files['file']

        # 如果用户没有选择文件，浏览器也会提交一个空的文件
        if file.filename == '':
            return 'No selected file'

        # 读取文件内容
        file_content = file.read().decode('utf-8')
        filename = file.filename.split('.')[0]

        result=DictionaryService.add_dict(filename,file_content)

        return jsonify(code=RET.OK, message=result['message'], data=result['data'])


        # # 处理文件内容
        # print(file_content)
        # print(type(file_content))
        #
        # return 'File content read successfully'

    # 删除词典
    @classmethod
    def delete_dictionary(cls,dictionary_id):
        if not dictionary_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        res = DictionaryService.delete_dict(dictionary_id)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 获取词典的单词
    @classmethod
    def get_words(cls, dictionary_id):
        if not dictionary_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        res = DictionaryService.get_words_by_dictionary_id(dictionary_id)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 删除词典的单词
    @classmethod
    def delete_word(cls,dictionary_id, word_id):
        if not dictionary_id or not word_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        res = DictionaryService.delete_word_from_dict(dictionary_id, word_id)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 将单词设置为该词典的题库
    @classmethod
    def set_word_as_test(cls,dictionary_id, word_id):  # 默认为 0 (不设置为题库)

        result = DictionaryService.set_word_as_test(dictionary_id, word_id)
        return jsonify(result)

    # 取消单词设置为该词典的题库
    @classmethod
    def cancel_word_as_test(cls, dictionary_id, word_id):  # 默认为 0 (不设置为题库)

        result = DictionaryService.cancel_word_as_test(dictionary_id, word_id)
        return jsonify(result)

    # 根据名称查找词典
    @staticmethod
    def search_dictionaries_by_name():
        data = request.json
        name = data.get('name', '')

        if not name:
            return jsonify({'code': RET.PARAMERR, 'message': '名称不能为空', 'data': None})

        result = DictionaryService.search_dictionaries_by_name(name)
        return jsonify(result)

    # 根据单词或单词的一部分对某个词典进行查找
    @classmethod
    def search_words_in_dictionary(cls, dictionary_id):
        if not dictionary_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        data = request.json
        word = data.get('word', '')
        if not word:
            return jsonify(code=RET.PARAMERR, message='单词不能为空', data=None)

        res = DictionaryService.search_words_in_dictionary(dictionary_id, word)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 重命名词典
    @classmethod
    def rename_dictionary(cls, dictionary_id):
        if not dictionary_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('new_name', location='form', required=True, help='new_name参数类型不正确或缺失')

        kwargs = parser.parse_args()
        new_name = kwargs['new_name']

        res = DictionaryService.rename_dict(dictionary_id, new_name)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])



    # 生成测试题目
    @classmethod
    def generate_test_questions(cls, dictionary_id):
        num_questions = request.args.get('num_questions', default=5, type=int)
        result = DictionaryService.generate_test_questions(dictionary_id, num_questions)
        return jsonify(result), 200

